import React, {Component} from 'react';
import { NavLink } from 'react-router-dom';
import { Input,Space,Button ,Divider,message, Modal} from 'antd';

import './addManeger.css'
import axios from "../../utils/http"
import { UserOutlined,UnlockOutlined } from '@ant-design/icons';
import { EyeInvisibleOutlined, EyeTwoTone,BulbOutlined } from '@ant-design/icons';
const { confirm } = Modal;
export default class AddManeger extends Component {
    constructor(props) {
        super(props);
        this.state = {
            user:'',
            pwd:'',
        };
    }

    componentDidMount(){
        console.log(this.props.match.params.id)
        axios.get('/mm-power/administrators/queryAdministrators',{
            params:{
                adminId:4,
            }
        }).then(data=>{
            console.log(data.data.data)

          for(let i=0;i<data.data.data.length;i++){
              if(data.data.data[i].adminId==this.props.match.params.id){
                  console.log(data.data.data[i].adminUsername)
                  this.setState({
                          user:data.data.data[i].adminUsername,
                          pwd:data.data.data[i].adminPassword
                      })
          }else{

              }
          }
        })

        // this.setState({
        //     user:this.props.location.state.name.adminUsername,
        //     pwd:this.props.location.state.name.adminPassword
        // })
    }
    userText=(e)=>{
        this.setState({
            user:e.target.value
        })
    }
    pwdText=(e)=>{
        this.setState({
            pwd:e.target.value
        })
    }
    addManeger=()=>{
        let that=this
        axios.post('/mm-power//administrators/updateAdministrators',{
            "adminUsername":this.state.user,
            "adminPassword":this.state.pwd
        }).then(data=>{

            if(data.data.code){
                new Promise((resolve, reject) => {

                    message.success('修改成功')

                    resolve(200)


                }).then(data=>{
                    setTimeout(()=>{
                        confirm({
                            title: '您是否需要回到管理员管理界面',
                            icon:<BulbOutlined />,

                            onOk() {
                                that.props.history.push('/Home/quanXian/Maneger')
                            },
                            onCancel() {
                                console.log('Cancel');
                            },
                        });
                    },2000)

                })


            }else{
                message.error('修改失败')
            }

        }).catch(err=>{
            console.log(err)
            message.error('服务器报错')
        })
    }
    render() {
        return (
            <div className='addManege'><h1>修改管理员</h1>
                <div className='biaoTi'>
                    <NavLink to='/Home/quanXian/Maneger'><span className='xiaoWei'>管理</span></NavLink>
                    <NavLink to='/Home/quanXian/AddManeger'><span className='spanT xiaoWei'>修改管理员</span></NavLink>
                </div>

                <div className='kuang'>
                    <div className='wen'>用户名</div>
                    <Input size="large" value={this.state.user}
                           onChange={this.userText}
                           placeholder="请输入用户名" prefix={<UserOutlined />} />


                    <br/>
                    <Space direction="vertical">

                        <div className='wen'>密码</div> <Input.Password
                        onChange={this.pwdText}
                        value={this.state.pwd}
                        placeholder="请输入密码"
                        prefix={<UnlockOutlined />}
                        iconRender={visible => (visible ? <EyeTwoTone /> : <EyeInvisibleOutlined />)}
                    />
                    </Space>
                    <Divider></Divider>
                    <Button type="primary" onClick={this.addManeger}>确认</Button>

                </div>


            </div>
        )
    }
}